package game.module.item.dao;

import game.module.item.beans.CharacterGoods;
import game.module.item.beans.GoodsDataEntry;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

@Repository
public class GoodsDao {

	public static final Logger logger = LoggerFactory.getLogger(GoodsDao.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<CharacterGoods> getCharacterGoodsListByPosition(int characterId, int from, int to) throws SQLException {
		return jdbcTemplate.query("select * from t_character_goods where f_character_id=? and f_position >=? and f_position <=?", new Object[] { characterId, from, to },
				new CharacterGoodsExtractor());
	}

	public List<CharacterGoods> getCharacterAvatarGoodsList(int characterId) throws SQLException {
		return jdbcTemplate.query("select * from t_character_goods where f_character_id=? and f_position in(1,2,3)", new Object[] { characterId }, new CharacterGoodsExtractor());
	}

	public List<CharacterGoods> getCharacterGoodsList(int characterId) throws SQLException {
		return jdbcTemplate.query("select * from t_character_goods where f_character_id=?", new Object[] { characterId }, new CharacterGoodsExtractor());
	}

	public int updateAll(GoodsDataEntry record) throws SQLException {
		return jdbcTemplate.update("update t_character_goods set f_character_id=?,f_goodmodel_id=?,f_position=?", record.getCharacterId(), record.getGoodmodelId(),
				record.getPosition());
	}

	public int updatePositionAndCount(short pos, int count, int goodsId) throws SQLException {
		int ret = jdbcTemplate.update("update t_character_goods set f_position=?,f_count=? where f_id=?", pos, count, goodsId);
		return ret;
	}

	public void insert(final GoodsDataEntry record) throws SQLException {
		jdbcTemplate.update(
				"insert into t_character_goods(f_character_id,f_goodmodel_id,f_position,f_count,f_bind,f_curr_durability,f_pin) values(?,?,?,?,?,?,?)",
				new PreparedStatementSetter() {

					@Override
					public void setValues(PreparedStatement ps) throws SQLException {
						ps.setInt(1, record.getCharacterId());
						ps.setInt(2, record.getGoodmodelId());
						ps.setShort(3, record.getPosition());
						ps.setInt(4, record.getCount());
						ps.setByte(5, record.getBind());
						ps.setInt(6, record.getCurrDurability());
						ps.setInt(7, record.getPin());
					}
				});
	}

	public int deleteById(int id) throws SQLException {
		int ret = jdbcTemplate.update("delete from t_character_goods where f_id = ?", id);
		return ret;
	}

	public int deleteByPosition(int roleId, int fromPos, int targetpos) throws SQLException {
		int ret = jdbcTemplate.update("delete from t_character_goods where f_character_id=? and f_position >=? and f_position <=?", roleId, fromPos, targetpos);
		return ret;
	}

	public List<CharacterGoods> getAllHeroBodyEquipments() throws SQLException {
		return jdbcTemplate.query("select * from t_character_goods WHERE f_position>=1 and f_position <=13", new CharacterGoodsExtractor());
	}

	public List<CharacterGoods> getAllHeroBodyGoodids() throws SQLException {
		return jdbcTemplate.query("select * from t_character_goods WHERE f_position>=1 and f_position <=13", new CharacterGoodsExtractor());
	}

	static class CharacterGoodsExtractor implements ResultSetExtractor<List<CharacterGoods>> {

		@Override
		public List<CharacterGoods> extractData(ResultSet rs) throws SQLException, DataAccessException {
			List<CharacterGoods> retList = new ArrayList<CharacterGoods>();
			while (rs.next()) {
				CharacterGoods characterGoods = new CharacterGoods();
				characterGoods.setId(rs.getInt("f_id"));
				characterGoods.setCharacterId(rs.getInt("f_character_id"));
				characterGoods.setGoodmodelId(rs.getInt("f_goodmodel_id"));
				characterGoods.setPosition(rs.getShort("f_position"));
				characterGoods.setCount(rs.getInt("f_count"));
				characterGoods.setBind(rs.getByte("f_bind"));
				characterGoods.setCurrDurability(rs.getInt("f_curr_durability"));
				characterGoods.setMaxDurability(rs.getInt("f_max_durability"));
				characterGoods.setPin(rs.getInt("f_pin"));
				retList.add(characterGoods);
			}
			return retList;
		}

	}

}
